<?php

namespace App\Dao\User;

use Yj\Daos\BaseDao;
use App\Model\User\WechatUser;
use Hyperf\Database\Model\Builder;
use Yj\Daos\Verify;
use Hyperf\Utils\Collection;

/**
 * @var WechatUser
 */
class WechatUserDao extends BaseDao
{
    public function DaoWhere(array $params)
    {
        return $this->getDaoQuery($params, function (Verify $verify) {
            $verify("id", function (Builder $query, $id) {
                $query->where("id", $id);
            });
        });

    }

    public function lists(Collection $params)
    {

        $data = $this->DaoWhere($params)->orderByDesc("id")->getList();
        return $data;

    }

    public function add(Collection $params)
    {

        $data = $this->DaoWhere($params)->create($params->toArray());
        return $data;

    }


    public function edit(int $id, Collection $params)
    {

        $data = $this->DaoWhere(["id" => $id])->firstOrError();
        $data->fill($params->toArray());
        return $data->save();

    }

    public function getOpenid(int $userId)
    {
        $data = $this->where(
            'uid', $userId
        )->firstOrError();
        return $data->openid;
    }
}
